package com.whfc.mach.manager.warn.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.base.enums.AppModuleType;
import com.whfc.base.param.AlarmMessageParam;
import com.whfc.common.util.DateUtil;
import com.whfc.mach.dao.AppMachWarnRecordMapper;
import com.whfc.mach.dto.warn.MachWarnCheckDTO;
import com.whfc.mach.dto.warn.MachWarnRuleDTO;
import com.whfc.mach.entity.AppMachWarnRecord;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.mach.manager.warn.AbstractWarnManager;
import com.whfc.mach.manager.warn.AppWarnManager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 设备速度报警规则
 *
 * @author guodong_sun
 */
@Service(value = "machSpeedWarnCheckManagerImpl")
public class MachSpeedWarnCheckManagerImpl extends AbstractWarnManager implements AppWarnManager {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String SPEED_WARN_CONTENT = "%s %s 在 %s,行驶速度>%s（km/h）,请您及时处理";

    /**
     * 最大速度限制
     */
    private static final Double MAX_SPEED_LIMIT = 45.0;

    private static final String PARAM_NAME = "value";

    @Autowired
    private AppMachWarnRecordMapper appMachWarnRecordMapper;

    @Override
    public boolean checkValue(MachWarnCheckDTO machWarnCheckDTO, MachWarnRuleDTO machWarnRuleDTO) {
        logger.info("检测设备速度报警规则,machWarnCheckDTO:{},machWarnRuleDTO:{}", machWarnCheckDTO.toString(), machWarnRuleDTO.toString());
        Double speed = machWarnCheckDTO.getSpeed();
        if (speed == null) {
            return false;
        }
        String ruleValueStr = JSON.parseObject(machWarnRuleDTO.getRuleParam()).getString(PARAM_NAME);
        if (StringUtils.isBlank(ruleValueStr)) {
            return false;
        }
        if (speed < MAX_SPEED_LIMIT && speed > Double.valueOf(ruleValueStr)) {
            machWarnCheckDTO.setTriggerValue(String.valueOf(speed));
            return true;
        }
        return false;
    }

    @Override
    public AlarmMessageParam generateWarnRecord(MachWarnCheckDTO machWarnCheckDTO, MachWarnRuleDTO machWarnRuleDTO) {
        String ruleValue = JSON.parseObject(machWarnRuleDTO.getRuleParam()).getString(PARAM_NAME);
        String triggerParam = JSON.toJSONString(machWarnCheckDTO);
        // 生成报警记录
        String objectId = String.valueOf(machWarnCheckDTO.getMachId());
        AppMachWarnRecord record = new AppMachWarnRecord();
        Date time = machWarnCheckDTO.getTriggerTime();
        record.setDeptId(machWarnCheckDTO.getDeptId());
        record.setRuleType(machWarnRuleDTO.getRuleType());
        record.setRuleId(machWarnRuleDTO.getRuleId());
        record.setTriggerObjectId(objectId);
        record.setTriggerTime(time);
        record.setRuleExpress(machWarnRuleDTO.getRuleExpress());
        record.setRuleParam(machWarnRuleDTO.getRuleParam());
        record.setTriggerParam(triggerParam);
        appMachWarnRecordMapper.insertSelective(record);
        logger.info("生成速度报警,appAlarm:{}", record);

        // 组装生成消息参数
        AlarmMessageParam alarmMessageParam = new AlarmMessageParam();
        String content = String.format(SPEED_WARN_CONTENT, machWarnCheckDTO.getMachTypeName(), machWarnCheckDTO.getMachCode(), DateUtil.formatDateTime(time), ruleValue);
        alarmMessageParam.setAlarmId(String.valueOf(record.getId()));
        alarmMessageParam.setContent(content);
        alarmMessageParam.setModuleType(AppModuleType.MACH.value());
        alarmMessageParam.setDeptId(record.getDeptId());
        alarmMessageParam.setRuleId(record.getRuleId());
        alarmMessageParam.setTime(time);
        alarmMessageParam.setTitle(AppMachWarnRuleType.SPEED.tittle());
        alarmMessageParam.setTriggerObjectId(objectId);
        alarmMessageParam.setRuleType(machWarnRuleDTO.getRuleType());
        return alarmMessageParam;
    }


}
